package com.example.elderlycare.service.entity;

import com.example.elderlycare.dao.entity.Department;
import lombok.Builder;
import lombok.Data;
import lombok.experimental.Tolerate;

import java.util.ArrayList;
import java.util.List;

@Data
@Builder
public class DepartmentTreeResponseBody {

    @Data
    @Builder
    public static class DepartmentTreeItem {
        private Integer id;
        private String name;
        private String manager;
        private String phone;
        private Integer fatherId;
        private List<DepartmentTreeItem> children;

        @Tolerate
        DepartmentTreeItem() {
        }
    }

    @Data
    @Builder
    public static class DepartmentTreeSetItem {
        private Integer nurseId;
        private String nurse;
        private List<DepartmentTreeItem> children;

        @Tolerate
        DepartmentTreeSetItem() {
        }
    }

    private List<DepartmentTreeSetItem> items;

    public static List<DepartmentTreeItem> generateTree(List<Department> params, Integer source) {
        List<DepartmentTreeItem> treeItems = new ArrayList<>();
        for (Department department : params) {
            if (department.getFatherId().equals(source)) {
                List<DepartmentTreeItem> child = generateTree(params, department.getId());
                DepartmentTreeItem currentItem = DepartmentTreeItem.builder().
                        id(department.getId()).
                        name(department.getName()).
                        manager(department.getManager()).
                        phone(department.getPhone()).
                        fatherId(department.getFatherId()).
                        children(child).
                        build();
                treeItems.add(currentItem);
            }
        }
        return treeItems;
    }

    @Tolerate
    DepartmentTreeResponseBody() {
    }
}
